﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserAlbumDashboardController : DefaultController
	{
		public UserAlbumDashboardController(IUserAlbumService userAlbumService)
		{
			this.UserAlbumService = userAlbumService;
		}

		public IUserAlbumService UserAlbumService
		{
			get; 
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Index(int? page)
		{
			var albums = UserAlbumService.GetPaged(new UserAlbumSpecification
			{
				UserId = Identity.Id,
				Page = page,
				Limit = Settings.Users.AlbumPageLimit
			});

			var album = albums.FirstOrDefault();

			return album.CanView ? View(Views.Index, albums) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Create()
		{
			var album = UserAlbumService.Create();

			return album.CanCreate ? View(Views.Create, new UserAlbumCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Create(UserAlbumCreate value)
		{
			try
			{
				var album = UserAlbumService.Create();

				if (!album.CanCreate)
				{
					return NotAuthorized();
				}

				UserAlbumService.Insert(album, value);

				return Message(Routes.UserAlbumDashboardUpdate, new { id = album.Id }, Resources.Messages.UserAlbumCreated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Update(int id)
		{
			var album = UserAlbumService.GetById(id);

			if (album == null)
			{
				return Error(Resources.Messages.UserAlbumNotFound);
			}

			return album.CanUpdate || album.CanUpdateAny ? View(Views.Update, new UserAlbumUpdate(album)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Update(UserAlbumUpdate value)
		{
			try
			{
				var album = UserAlbumService.GetById(value.Id);

				if (album == null)
				{
					return Error(Resources.Messages.UserAlbumNotFound);
				}

				if (!album.CanUpdate && !album.CanUpdateAny)
				{
					return NotAuthorized();
				}

				UserAlbumService.Update(album, value);

				return Message(Routes.UserAlbumDashboardUpdate, new { id = album.Id }, Resources.Messages.UserAlbumUpdated);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Delete(int id)
		{
			var album = UserAlbumService.GetById(id);

			if (album == null)
			{
				return Error(Resources.Messages.UserAlbumNotFound);
			}

			return album.CanDelete || album.CanDeleteAny ? View(Views.Delete, new UserAlbumDelete(album)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Delete(UserAlbumDelete value)
		{
			try
			{
				var album = UserAlbumService.GetById(value.Id);

				if (album == null)
				{
					return Error(Resources.Messages.UserAlbumNotFound);
				}

				if (!album.CanDelete && !album.CanDeleteAny)
				{
					return NotAuthorized();
				}

				UserAlbumService.Delete(album);

				return RedirectToRoute(Routes.UserAlbumDashboardIndex);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Delete, value);
		}
	}
}